package cn.hf.mychat.controller;


import cn.hf.mychat.config.Result;
import cn.hf.mychat.entity.bo.RequestUser;
import cn.hf.mychat.entity.po.TbSysUser;
import cn.hf.mychat.entity.test.TestEntity;
import cn.hf.mychat.service.UserService;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.task.AsyncListenableTaskExecutor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@Slf4j
@RestController
@RequestMapping("")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    private final ApplicationEventPublisher applicationEventPublisher;
  /*  @Qualifier("applicationTaskExecutor")
    @Autowired*/
    private final AsyncListenableTaskExecutor asyncListenableTaskExecutor;

    @Value("${text.aa}")
    private String aa;

    @Value("${test.password}")
    private Integer password;

    @Value("${panpan.name}")
    private String name;

    //异步
    private final AsyncListenableTaskExecutor taskExecutor;
    /**
     * 注册
     * @return 注册状态
     */
    @PostMapping("register")
    @ResponseBody
    public ResponseEntity<?> register(@RequestBody @Valid RequestUser requestUser) {
        //TODO 正则验证格式
       Integer count = this.userService.register(requestUser);
       if (count == 1 ){
           return ResponseEntity.ok(Result.success(1));
       }
       return ResponseEntity.ok(Result.error("注册失败"));
    }

    @PostMapping("listener")
    public String listener(@RequestBody TestEntity entity) throws InterruptedException {
      applicationEventPublisher.publishEvent(entity);

        System.out.println("00000000000000000000");
      /*  Thread.sleep(5000);*/
        System.out.println(entity);
      return "完成";
    }

    @GetMapping("async")
    public String async(){
        //执行一个异步的方法。即时返回数据前端
        System.out.println(aa);
        System.out.println(password);
        System.out.println(name);
        userService.async();
        return "完成";
    }

    @GetMapping("ceshi")
    public ResponseEntity ceshi(){
        Map<String, Object> map = new HashMap<>();
        map.put("1","wwww");
        map.put("2","dddd");
        //TestEntity build = TestEntity.builder().name("来啦").password("你好").build();
       //map.put("用户",build);
        return ResponseEntity.ok(new Result<>(map));
    }

    @GetMapping("h")
    public ResponseEntity test(){
        System.out.println(asyncListenableTaskExecutor);
        Future<?> submit = this.asyncListenableTaskExecutor.submit(() -> {
            try {
                Thread.sleep(6000);
                System.out.println("执行完了");
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                log.info("错误");
            }
        });
        System.out.println(submit);
        return ResponseEntity.ok("成功");
    }

    //异步返回。时间sql的示例
    @GetMapping("feng")
    public String test1(){
        taskExecutor.execute(this.userService::findByTime
        );
        return "success";
    }

    //mybatis queryWrapper
    @PostMapping("queryWrapper")
    public ResponseEntity testWrapper(@RequestBody TbSysUser tbSysUser){
        List<TbSysUser> tbSysUsers = this.userService.testWrapper(tbSysUser);
        return ResponseEntity.ok(tbSysUsers);
    }

    //加解密
    @PostMapping("encrypt")
    public ResponseEntity encrypt(@RequestBody TbSysUser tbSysUser){
        this.userService.encrypt(tbSysUser);
        List<TbSysUser> all = this.userService.findAll();
        return ResponseEntity.ok(all);
    }
}
